# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\mailer\threaded_mailer.py
import logging, time
from queue import Queue, Empty as QueueEmpty
from helpers.mailer.base_mailer import Mailer
from helpers.mailer.sender.base import Sender
from helpers.mailer.renderer.base import Renderer

logger = logging.getLogger("helpers.mailer.threaded_mailer")


class ThreadedMailer(Mailer):
    def enqueue(
        self,
        from_address,
        data,
        user_id=None,
        subject=None,
        shard=None,
        notification_type=None,
        subject_template=None,
        html_template=None,
        txt_template=None,
        frontend_url=None,
        email=None,
        first_name=None,
        last_name=None,
    ):
        self.queue.put(
            dict(
                from_address=from_address,
                user_id=user_id,
                data=data,
                subject=subject,
                shard=shard,
                notification_type=notification_type,
                subject_template=subject_template,
                html_template=html_template,
                txt_template=txt_template,
                frontend_url=frontend_url,
                email=email,
                first_name=first_name,
                last_name=last_name,
            )
        )

    def __init__(self, shard, sender, renderer):
        super().__init__(shard=shard, sender=sender, renderer=renderer)
        self.queue = Queue()
        self.should_stop = False

    def stop(self):
        self.should_stop = True

    def run(self):
        logger.info("mailer worker started")
        while 1:
            if self.should_stop:
                logger.info("mailer worker exiting, bye.")
                return
                try:
                    mail_data = self.queue.get(timeout=1)
                except QueueEmpty:
                    time.sleep(1)
                    continue

                try:
                    self.send(**mail_data)
                except Exception as e:
                    logger.exception("exception during sending email %s", e)
                finally:
                    self.queue.task_done()
